import { _decorator,Prefab, Component, Node ,Canvas,instantiate,Vec3, Touch, director,utils  } from 'cc';
const { ccclass, property } = _decorator;

@ccclass('plane')
export class plane extends Component {

    startPosition:Vec3 = new Vec3()
    mouseXY = null
    @property(Prefab)
    bulletPre:Prefab = null
    
    onLoad(){
        this.startPosition = instantiate(this.node).position // 克隆
    }
    start() {
        // 监听down
        this.node.on(Node.EventType.TOUCH_START,(e:Touch)=>{
            this.mouseXY = e.getLocation()
        })
        // 监听移动
        this.node.on(Node.EventType.TOUCH_MOVE,(e:Touch)=>{
          
            let x = e.getLocation().x
            let y = e.getLocation().y

            let minx = x- this.mouseXY.x
            let miny = y- this.mouseXY.y
 
            let n = new Vec3(minx,miny,this.startPosition.z)
            let plantPos = n.add(this.startPosition)
            this.node.setPosition(plantPos)
        })
        this.node.on(Node.EventType.TOUCH_END,(e:Touch)=>{
            this.startPosition = instantiate(this.node).position // 克隆
        })

        // 攻击
        this.schedule(()=>{
            let bullet = instantiate(this.bulletPre)
         
            // director.getScene().children[0].addChild(bullet)
            bullet.setParent(this.node.getParent())
            let pos = this.node.getPosition()
            bullet.setPosition(new Vec3(pos.x,pos.y+60,0))
            

        },0.5);
    }

    update(deltaTime: number) {
        // 攻击
        
    }
}


